/*
 * This program is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this 
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
 * or from the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 Pentaho Corporation.  All rights reserved.
 *
 * @author Will Gorman (wgorman@pentaho.com)
 */
package org.pentaho.actioneditor.client.processpanels;

import org.pentaho.actionsequence.dom.IActionSequenceResource;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ResourcePanel {
  
  int selectedSourceRow = -1;
  final ResourceUI rui = new ResourceUI();
  TreeItem parentTreeItem;
  IActionSequenceResource resource;
  
  public ResourcePanel(IActionSequenceResource actionSequenceResource, TreeItem parentTreeItem) {
    this.parentTreeItem = parentTreeItem;
    this.resource = actionSequenceResource;
  }
  
  public VerticalPanel getRoot() {
    return rui.getRoot();
  }
  
  public void init() {
    
    rui.init();
    
    // display the item's details pane
    
    rui.getName().setText(resource.getName());
    rui.getLocation().setText(resource.getPath());
    rui.getMimeType().setText(resource.getMimeType());
    
    for (int i = 0; i < rui.getSourceType().getItemCount(); i++) {
      if (rui.getSourceType().getItemText(i).equals(resource.getType())) {
        rui.getSourceType().setSelectedIndex(i);    
      }
    }
    
    // updates
    
    rui.getSourceType().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        System.out.println("TYPE CHANGE: " + rui.getSourceType().getItemText(rui.getSourceType().getSelectedIndex()));
        resource.setType(rui.getSourceType().getItemText(rui.getSourceType().getSelectedIndex()));
      }
    });
    
    rui.getName().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        resource.setName(rui.getName().getText());
        parentTreeItem.setText(rui.getName().getText());
      }
    });
    
    rui.getLocation().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        resource.setPath(rui.getLocation().getText());
      }
    });
    
    rui.getMimeType().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        resource.setMimeType(rui.getMimeType().getText());
      }
    });
    

    
  }
}
